package com.hibernate;

import com.entity.*;
import com.utils.DataUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Demo01 {
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure("hibernate.cfg.xml")
                .build();
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();


         private List<Stock> stockList;
        private List<Box> boxes;
        private HashMap<Long, String> close;
        @AfterEach
        public void testAfter(){
                session.close();
        }

    @Test
    public void test01() {
        //HQL:是基于类的class名字、类的字段名字
        String sql = "select e from " + Employee.class.getName() + " e " + " order by e.empName, e.empNo ";

            Query<Employee> query = session.createQuery(sql);

            List<Employee> employees = query.getResultList();

            for (Employee emp : employees) {
                    System.out.println("Emp:" + emp.getEmpNo() + ":" + emp.getEmpName());
            }
    }

    @Test
    public void test02(){
            String sql = "Select e from " + Employee.class.getName() + " e " + " where e.department.deptNo=:deptNo ";

        Query<Employee> query = session.createQuery(sql);

        query.setParameter("deptNo","D10");

        List<Employee> employees = query.getResultList();

        for (Employee emp : employees) {
            System.out.println("Emp:" + emp.getEmpNo() + ":" + emp.getEmpName());
        }
    }

    @Test
    public void test03(){
        String sql = "Select e.empId, e.empNo, e.empName from " + Employee.class.getName() + " e ";

        Query<Object[]> query = session.createQuery(sql);

        List<Object[]> data = query.getResultList();

        for (Object[] emp : data) {
            System.out.println("Emp Id: " + emp[0]);
            System.out.println("    Emp No: " + emp[1]);
            System.out.println("    Emp Name: " + emp[2]);
        }
    }

    @Test
    public void test04(){
            String sql = "Select new " + ShortEmpInfo.class.getName()
                    + "(e.empId, e.empNo, e.empName)" + " from "
                    + Employee.class.getName() + " e ";

        Query<ShortEmpInfo> query = session.createQuery(sql);

        List<ShortEmpInfo> employees = query.getResultList();

        for (ShortEmpInfo emp : employees) {
            System.out.println("Emp: " + emp.getEmpNo() + " : " + emp.getEmpName());
        }
    }

    @Test
    public void test06(){
        Department department = null;
        Employee emp = null;
        try {
            session.getTransaction().begin();

            Long maxEmpId = DataUtils.getMaxEmpId(session);
            Long empId = maxEmpId + 1;

            department = DataUtils.findDepartment(session, "D10");

            emp = new Employee();
            emp.setEmpId(empId);
            emp.setEmpNo("E" + empId);
            emp.setEmpName("Name " + empId);
            emp.setJob("Coder");
            emp.setSalary(1000f);
            emp.setManager(null);
            emp.setHideDate(new Date());
            emp.setDepartment(department);

            session.persist(emp);
            session.getTransaction().commit();
        }catch (Exception e){
            session.getTransaction().rollback();
            System.out.println("回滚事务:"+e.getMessage());
        }
        System.out.println("Emp No: " + emp.getEmpNo());
    }

    @Test
    public void test07(){
            Department department = null;

            try {
                session.getTransaction().begin();
                department = DataUtils.findDepartment(session,"D10");
                System.out.println("- First change Location");

                // Changing something on Persistent object.
                department.setLocation("Chicago " + System.currentTimeMillis());

                System.out.println("- Location = " + department.getLocation());

                System.out.println("- Calling flush...");

                // Use session.flush () to actively push the changes to the DB.
                // It works for all changed Persistent objects.
                session.flush();

            }catch (Exception e){
                e.printStackTrace();
                session.getTransaction().rollback();
            }

            session = sessionFactory.openSession();
            try {
                session.getTransaction().begin();
                System.out.println("- Finding Department deptNo = D10...");
                department = DataUtils.findDepartment(session,"D10");

                department = DataUtils.findDepartment(session, "D10");

                // Print out location
                System.out.println("- D10 Location = " + department.getLocation());

                session.getTransaction().commit();
            }catch (Exception e) {
                e.printStackTrace();
                session.getTransaction().rollback();
            }
    }

    @Test
    public void appleTest(){


        Stock apple1 = new Stock();
        apple1.setNo(380l);
        apple1.setName("bird");
        apple1.setGood("坏了");
        Stock apple2 = new Stock();
        apple2.setNo(12l);
        apple2.setName("cat");
        apple2.setGood("完好");
        Stock apple3 = new Stock();
        apple3.setNo(10000l);
        apple3.setName("golden");
        apple3.setGood("完好");
        Stock apple4 = new Stock();
        apple4.setNo(36l);
        apple4.setName("cow");
        apple4.setGood("坏了");
        Stock apple5 = new Stock();
        apple5.setName("tree");
        apple5.setGood("完好");
        Stock apple6 = new Stock();
        apple6.setNo(1001l);
        apple6.setName("alibaba");
        apple6.setGood("完好");
        Stock apple7 = new Stock();
        apple7.setNo(20000l);
        apple7.setName("fish");
        apple7.setGood("完好");


        session.persist(apple1);
        session.persist(apple2);
        session.persist(apple3);
        session.persist(apple4);
        session.persist(apple5);
        session.persist(apple6);

        session.close();
        System.out.println("success");
    }
}
